package org.example.lotterysystem.service.activitystatus.operater;

import org.example.lotterysystem.dao.dataobject.ActivityUserDO;
import org.example.lotterysystem.dao.mapper.ActivityUserMapper;
import org.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.example.lotterysystem.service.enmus.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class UserOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityUserMapper activityUserMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    /**
     * 判断活动关联用户是否需要更新。
     * @param convertActivityStatusDTO
     * @return
     */
    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityUserStatusEnum targetUserStatus = convertActivityStatusDTO.getTargetUserStatus();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        if (null == activityId|| userIds.isEmpty()||targetUserStatus==null) {
            return false;
        }
        //从数据库里面查询数据，目标表为：活动用户关联表。
        List<ActivityUserDO> activityUserDOList =activityUserMapper.batchSelectByAUIds(activityId,userIds);
        if(activityUserDOList==null||activityUserDOList.isEmpty()){
            return false;
        }
        //下面就是将从数据库里面查询的数据与我封装的数据进行对比
        for(ActivityUserDO activityUserDO:activityUserDOList){
            if(activityUserDO.getStatus().equalsIgnoreCase(convertActivityStatusDTO.getTargetUserStatus().name())){
                return  false;
            }
        }

        return true;
    }

    /**
     * 活动关联用户转换，更新数据库表
     * @param convertActivityStatusDTO
     * @return
     */
    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityUserStatusEnum targetUserStatus = convertActivityStatusDTO.getTargetUserStatus();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        try{
            activityUserMapper.batchUpdateStatus(activityId,targetUserStatus.name(),userIds);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
